package com.zhang.sql;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @title:
 * @author: zhang
 * @date: 2022/2/13 19:54
 */
public class StreamToTableProcessingTime {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        SingleOutputStreamOperator<Tuple2<String, String>> streamOperator = env
                .readTextFile("src/main/resources/OrderLog.csv")
                .map(new MapFunction<String, Tuple2<String, String>>() {
                    @Override
                    public Tuple2<String, String> map(String value) throws Exception {
                        String[] fields = value.split(",");
                        return Tuple2.of(
                                fields[0],
                                fields[1]
                        );
                    }
                });

        Table table = tableEnv.fromDataStream(streamOperator,
                $("userId"),
                $("type"),
                $("pt").proctime());  //处理时间

        Table select = table.select($("userId"),
                $("type"),
                $("pt"));

        tableEnv.toAppendStream(select, Row.class).print();

        //table.printSchema();

        env.execute();


    }
}
